<?php
class Admin_VisaController extends Apps_Controller_Action_Admin {

	//LIST
	public function indexAction() {
		$this->actResolve('act_delete',
						  'delete', null, null, array('id' => $this->_getParam('item_checker')),
					 	  true);

		$tbVisa = new M_DbTable_Visa();
		$grid 	= new Apps_Plugin_Grid('VisaGrid');

		$grid->setData($tbVisa->getList())
			 ->setTableCaption('Article about visa service')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Titile (En)',
			 			  'dataField'	=> 'en_title'),
			 		array('title'		=> 'Title (Vi)',
			 			  'dataField'	=> 'title'),
			 		array('title'		=> 'Description',
			 			  'dataField'	=> 'description',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Created Date',
			 			  'dataField'	=> 'created_date',
			 			  'class'		=> 'center'),
			 		array('title'       => 'Edit',
			 			  'html'        => array('<a href="/admin/visa/edit?id=%s"><i class="icon-edit" title="Sửa"></i></a>',
			 			  				   array('id')),
			 			  'width'       => '2%')
			 		));
		$this->view->grid = $grid;
	}

	//ADD
	public function addAction() {
		$rq 		= $this->_request;
		$f 			= new Admin_Form_AddVisa();
		$tbArticle 	= new M_DbTable_Article();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				// Upload thumbnail
				$uploader = new Apps_Plugin_Upload_File();
				$uploader->setMaxQuantity(1)
						 ->setSubCategory(Apps_Plugin_Upload_File::CAT_ARTICLE);
				$thumbnail = ($uploaded = $uploader->upload())
						 	 ? $uploaded['thumbnail']['uri']
						 	 : null;

				$data = $f->getValues();
				$data['thumbnail'] = $thumbnail;

				$this->_flashMessenger->addMessage(
					$tbArticle->addArticle($data, null, $tbArticle::REFERENCE_TYPE_VISA)
					? self::$msgAddSuccess
					: self::$msgAddFailed
				);
				$this->_helper->redirector('index', null, null);
			}
		}
		$this->view->form = $f;
	}

	//EDIT Visa
	public function editAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		$tbArticle 	= new M_DbTable_Article();

		$visa 	= $tbArticle->fetchRow('id = '. $id);
		if (!$id || !$visa) {
			$this->_helper->redirector('index');
		}

		$f = new Admin_Form_AddVisa();
		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				// Upload thumbnail - replace thumbnail if changed
				$thumbnail = $visa['thumbnail'];
				if(Apps_Plugin_Upload_File::isUploaded('thumbnail')) {
					$uploader = new Apps_Plugin_Upload_File();
					$uploader->setMaxQuantity(1)
							 ->setSubCategory(Apps_Plugin_Upload_File::CAT_ARTICLE);

					$uploaded = $uploader->upload();
					if($uploaded) {
						unlink(Apps_Plugin_Upload_File::getPath($thumbnail)); // Delete ex-thumbnail
						$thumbnail = $uploaded['thumbnail']['uri'];
					}
				}

				$data = $f->getValues();
				$data['thumbnail'] = $thumbnail;

				if ($tbArticle->safelyUpdate($data, 'id ='. $id)) {
					$this->_flashMessenger->addMessage(self::$msgUpdateSuccess);
				} else {
					$this->_flashMessenger->addMessage(self::$msgUpdateFailed);
				}
				$this->_helper->redirector('index');
			}
		}
		$f->setLegend('Update article');
		$f->populate($visa->toArray());
		$this->view->form = $f;
		$this->render('add');
	}

	//DELETE
	public function deleteAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		if (!$id) {
			$this->_helper->redirector('index');
		}

		$tbArticle = new M_DbTable_Article();
		if (is_numeric($id)) {
			$article = $tbArticle->fetchRow('id = '. $id);
			if ($tbArticle->safelyDelete('id ='. $id)) {
				unlink(Apps_Plugin_Upload_File::getPath($article['thumbnail']));
				$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
			} else {
				$this->_flashMessenger->addMessage(self::$msgDeleteError);
			}
		} elseif (is_array($id)) {
			foreach ($id as $v) {
				$article = $tbArticle->fetchRow('id = '. $id);
				if ($tbArticle->safelyDelete('id ='. $v)) {
					unlink(Apps_Plugin_Upload_File::getPath($article['thumbnail']));
				}
			}
		}


		$this->_helper->redirector('index');
	}

}